import BScroll from "@better-scroll/core";
import Pullup from "@better-scroll/pull-up";
import ObserverDom from "@better-scroll/slide";
BScroll.use(Pullup);
BScroll.use(ObserverDom);
import { ref, onMounted, onUnmounted } from "vue";
export default function usePullUpload(requestData, preventPullUpload) {
  const scroll = ref(null);
  const rootRef = ref(null);
  const isPullLoad = ref(false);
  onMounted(() => {
    const scrollVal = (scroll.value = new BScroll(rootRef.value, {
      pullUpLoad: true,
      observeDOM: true,
      click: true,
    }));
    scrollVal.on("pullingUp", pullingUpHandler);
    async function pullingUpHandler() {
      if (preventPullUpload.value) {
        scrollVal.finishPullUp();
        return;
      }
      isPullLoad.value = true;
      await requestData();
      scrollVal.finishPullUp();
      scrollVal.refresh();
      isPullLoad.value = false;
    }
  });
  onUnmounted(() => {
    // scroll.value.destory();
  });

  return {
    scroll,
    rootRef,
    isPullLoad,
  };
}
